<div class="box box-solid no-margin box-show">
    <div class="box-body">
        <div class="box-body table-responsive no-padding">
            <table class="table table-hover">
                <thead>
                <tr>
                    <th><input type="checkbox" class="grid-select-all" />&nbsp;</th>
                    <th>站点</th>
                    <th>SKU</th>
                    <th>产品名称</th>
                    <th>在库</th>
                    <th>挪用数量</th>
                </tr>
                </thead>
                <tbody>
                @foreach($transferPs as $row)
                <tr>
                    <td><input type="checkbox" class="grid-row-checkbox" data-id="{{ $row->id }}" /></td>
                    <td>{{ $row->area->area ?? '' }}</td>
                    <td>{{ $row->sku->psku_code ?? '' }}</td>
                    <td>{{ $row->sku->psku_cn ?? '' }}</td>
                    <td>{{ $row->instant }}</td>
                    <td>
                        <input type="hidden" name="oarea_id" value="{{ $row->area_id}}">
                        <input type="text" class="transfer_num_"
                               style="width: 100px; text-align: right;"
                               name="transfer_num"
                               data-value="{{ $row->instant }}"
                               value="{{ 0 }}">
                    </td>
                </tr>
                @endforeach
                </tbody>
            </table>
        </div>
    </div>
</div>
<div class="box-body fields-group">
    <div class="form-group">
        <label class="col-sm-3 control-label"></label>
        <div class="col-sm-5">
            <label style="padding-top: 8px;" for="iarea_id" class="col-sm-3 control-label">申请站点</label>
            <div class="col-sm-8">
                <select id="iarea_id" name="iarea_id">
                    {{ getOptions(\App\Models\Basics\SysIArea::options()) }}
                </select>
                <span class="help-block"><i class="fa fa-info-circle"></i>&nbsp;库存将挪至此站点</span>
            </div>
        </div>
    </div>
</div>
<div style="margin-top: 10px;">
    <div class="box-body fields-group">
        <div class="form-group">
            <div class="col-sm-10" style="margin-left: 25%">
                <button type="button" class="btn btn-primary" id="transfer-a-confirm-submit">提交</button>
            </div>
        </div>
    </div>
</div>
<script language="JavaScript" type="text/javascript">
    $(function () {
        $('#iarea_id').select2({placeholder: '申请站点', allowClear: true, width: '220px'});
        $('.grid-row-checkbox').iCheck({checkboxClass:'icheckbox_minimal-blue'}).on('ifChanged', function () {
            if (this.checked) {
                $(this).closest('tr').css('background-color', '#ffffd5');
            } else {
                $(this).closest('tr').css('background-color', '');
            }
        });
        $('.grid-select-all').iCheck({checkboxClass:'icheckbox_minimal-blue'});
        $('.grid-select-all').on('ifChanged', function(event) {
            if (this.checked) {
                $('.grid-row-checkbox').iCheck('check');
            } else {
                $('.grid-row-checkbox').iCheck('uncheck');
            }
        });

        var selectedRows = function () {
            var param = {};
            var selected = [];
            var detail = {};
            $('.grid-row-checkbox:checked').each(function(index){
                var id = $(this).data('id');
                selected.push(id);
                var indexArr = {};
                indexArr['id'] = id;
                var tr = $(this).closest('tr');
                indexArr['oarea_id'] = tr.find('input[name="oarea_id"]').val();
                indexArr['transfer_num'] = tr.find('input[name="transfer_num"]').val();
                detail[index] = indexArr;
            });
            param.selected = selected;
            param.detail = detail;
            return param;
        };

        $('#transfer-a-confirm-submit').on('click', function() {
            $(this).prop('disabled',true);
            var iAreaId = $('#iarea_id').val();
            if(!iAreaId){
                $(this).prop('disabled',false);
                swal('请选择申请站点', '', 'error');
                return false;
            }
            var pAreaId = "{{ $areaId }}";
            if(iAreaId == pAreaId){
                $(this).prop('disabled',false);
                swal('申请站点不能为PUBLIC', '', 'error');
                return false;
            }
            var areaTempId = "{{ $areaTempId }}";
            if(iAreaId == areaTempId){
                $(this).prop('disabled',false);
                swal('申请站点不能为TEMP', '', 'error');
                return false;
            }
            var param = selectedRows();
            var ids = param.selected;
            if(ids.length == 0) {
                $(this).prop('disabled',false);
                swal('请选择单据', '', 'error');
                return false;
            }
            var detail = param.detail;
            $.ajax({
                method: 'post',
                url: '/xqc/inventory/transfer-a-ajax',
                data: {
                    _token:"{{ csrf_token() }}",
                    action:"transfer",
                    param:{ iarea_id:iAreaId,detail:detail }
                },
                success: function (data) {
                    if (typeof data === 'object') {
                        if (data.status) {
                            toastr.success('提交成功 !');
                            $.pjax.reload('#transfer-a-confirm');
                        } else {
                            swal(data.message, '', 'error');
                        }
                    }
                }
            });
        });
        $(".transfer_num_").inputmask("integer",
            {
                onKeyValidation: function () {
                    var v = $(this).data('value');
                    if($(this).val() < 0 || $(this).val() > v){
                        $(this).val(0);
                    }
                }
            }
        );
    });
</script>
